package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"
	"wechat/wechatrpc/internal/svc"
	"wechat/wechatrpc/wechatrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *wechatrpc.LoginRequest) (*wechatrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line

	order := mysql.Order{
		Name:     in.Name,
		Tel:      in.Tel,
		Password: in.Password,
	}
	mysql.DB.Debug().Where("name=?", in.Name).Find(&order)
	if order.ID == 0 {
		return nil, fmt.Errorf("账户不存在,亲先去注册")
	}
	if in.Password != order.Password {
		return nil, fmt.Errorf("密码错误")
	}

	return &wechatrpc.LoginResponse{
		Id: int64(order.ID),
	}, nil
}
